package com.ruoyi.mobile.api;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qsng.domain.*;
import com.ruoyi.qsng.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息相关
 *
 * @author gk
 * @date 2023-06-19
 */
@Api(tags = "用户信息相关")
@RestController
@RequestMapping
public class UserController {

    @Autowired
    private IKUsersService kUsersService;

    @Autowired
    private IKActivityCollectService kActivityCollectService;

    @Autowired
    private IKCourseCollectService kCourseCollectService;

    @Autowired
    private IKActivityPurchaseService kActivityPurchaseService;

    @Autowired
    private IKCoursePurchaseService kCoursePurchaseService;

    @Autowired
    private IKAgeGroupService kAgeGroupService;

    @Autowired
    private IKCourseLocationService kCourseLocationService;

    @Autowired
    private IKCourseClassesService kCourseClassesService;

    @Autowired
    private IKClassCategoryService kClassCategoryService;

    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/weixin/getUser")
    @ApiOperation(value = "获取用户信息详细信息")
    public AjaxResults getInfo()
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        KUsers kUsers=kUsersService.selectKUsersById(String.valueOf(userId));

        //活动
        KActivityCollect  kActivityCollect  =new KActivityCollect();
        kActivityCollect.setUserId(String.valueOf(userId));
        kActivityCollect.setIsCollect(Long.valueOf(1));
        List<KActivityCollect> KActivityCollectList=kActivityCollectService.selectKActivityCollectList(kActivityCollect);
        kUsers.setActivityNum(Long.valueOf(KActivityCollectList.size()));

        //课程
        KCourseCollect kActivityCollect1  =new KCourseCollect();
        kActivityCollect1.setUserId(String.valueOf(userId));
        kActivityCollect1.setIsCollect(Long.valueOf(1));
        List<KCourseCollect> KCourseCollectList=kCourseCollectService.selectKCourseCollectList(kActivityCollect1);
        kUsers.setCourseNum(Long.valueOf(KCourseCollectList.size()));

        return AjaxResults.success(kUsers);
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/weixin/updateUser")
    @ApiOperation(value = "修改用户信息")
    public AjaxResults edit( KUsers kUsers)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        kUsers.setId(String.valueOf(userId));
        return AjaxResults.success(kUsersService.updateKUsers(kUsers));
    }

    /**
     * 获取活动收藏
     */
    @GetMapping(value = "/weixin/getActivity")
    @ApiOperation(value = "获取活动收藏")
    public AjaxResults getActivity(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,KActivityCollect  kActivityCollect)
    {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);


        PageHelper.startPage(pageNum, pageSize);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        kActivityCollect.setUserId(String.valueOf(userId));
        kActivityCollect.setIsCollect(Long.valueOf(1));
        List<KActivityCollect>  kActivityCollects=kActivityCollectService.selectKActivityCollectList(kActivityCollect);
        for (KActivityCollect kActivity1:kActivityCollects) {

            kActivity1.setRemainingQuota2(Math.toIntExact(kActivity1.getTotalQuota() - kActivity1.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kActivity1.getAgeGroup()) ){
                    kActivity1.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }
        }
        return AjaxResults.success(new PageSerializable<>(kActivityCollects));
    }

    /**
     * 获取课程收藏
     */
    @GetMapping(value = "/weixin/getCourse")
    @ApiOperation(value = "获取课程收藏")
    public AjaxResults getCourse( @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,KCourseCollect kActivityCollect)
    {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);

        PageHelper.startPage(pageNum, pageSize);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
        kActivityCollect.setUserId(String.valueOf(userId));
        kActivityCollect.setIsCollect(Long.valueOf(1));
        List<KCourseCollect> kCourseCollects =kCourseCollectService.selectKCourseCollectList(kActivityCollect);
        for (KCourseCollect kCourseCollect: kCourseCollects) {
            kCourseCollect.setRemainingQuota2(Math.toIntExact(kCourseCollect.getTotalQuota() - kCourseCollect.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kCourseCollect.getAgeGroup()) ){
                    kCourseCollect.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }
        }
        return AjaxResults.success(new PageSerializable<>(kCourseCollects));
    }

    /**
     * 获取活动报名
     */
    @GetMapping(value = "/weixin/getActivityPurchase")
    @ApiOperation(value = "获取活动报名")
    public AjaxResults getActivityPurchase(            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                       @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                       KActivityPurchase kActivityPurchase)
    {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);
        PageHelper.startPage(pageNum, pageSize);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
//        KActivityPurchase kActivityPurchase  =new KActivityPurchase();
        kActivityPurchase.setUserId(String.valueOf(userId));
        List<KActivityPurchase> list=kActivityPurchaseService.selectKActivityPurchaseList(kActivityPurchase);
        for (KActivityPurchase kActivityPurchase1:list) {
            kActivityPurchase1.setRemainingQuota2(Math.toIntExact(kActivityPurchase1.getTotalQuota() - kActivityPurchase1.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kActivityPurchase1.getAgeGroup()) ){
                    kActivityPurchase1.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }
        }
        return AjaxResults.success(new PageSerializable<>(list));
    }

    /**
     * 获取已购课程
     */
    @GetMapping(value = "/weixin/getPurchasedCourses")
    @ApiOperation(value = "获取已购课程")
    public AjaxResults getPurchasedCourses(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,KCoursePurchase kCoursePurchase)
    {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);


        PageHelper.startPage(pageNum, pageSize);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户id失败！");
//        KActivityPurchase kActivityPurchase  =new KActivityPurchase();
        kCoursePurchase.setUserId(String.valueOf(userId));
        List<KCoursePurchase>  kCoursePurchases=kCoursePurchaseService.selectKCoursePurchaseListForApp(kCoursePurchase);
        for (KCoursePurchase kCoursePurchase1 :kCoursePurchases) {
            kCoursePurchase1.setRemainingQuota2(Math.toIntExact(kCoursePurchase1.getTotalQuota() - kCoursePurchase1.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kCoursePurchase1.getAgeGroup()) ){
                    kCoursePurchase1.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }
        }
        return AjaxResults.success(new PageSerializable<>(kCoursePurchases));
    }

    /**
     * 获取已购课程详情
     */
    @GetMapping(value = "/weixin/getPurchasedCoursesById/{id}")
    @ApiOperation(value = "获取已购课程详情")
    public AjaxResults getPurchasedCoursesById(@PathVariable("id") String id) {
        KCoursePurchase kCoursePurchase=  kCoursePurchaseService.selectKCoursePurchaseById(id);
        kCoursePurchase.setRemainingQuota2(Math.toIntExact(kCoursePurchase.getTotalQuota() - kCoursePurchase.getSold()));
        kCoursePurchase.setActivityVenueApp(kCourseLocationService.selectKCourseLocationById(kCoursePurchase.getCourseLocation()).getName());
        kCoursePurchase.setAgeApp(kAgeGroupService.selectKAgeGroupById(kCoursePurchase.getAgeGroup()).getName());
        kCoursePurchase.setClassesApp(kCourseClassesService.selectKCourseClassesById(kCoursePurchase.getCourseClasses()).getName());
        kCoursePurchase.setClassApp(kClassCategoryService.selectKClassCategoryById(kCoursePurchase.getClassCategory()).getName());
        return   AjaxResults.success(kCoursePurchase);
    }

    /**
     * 获取活动报名详情
     */
    @GetMapping(value = "/weixin/getActivityById/{id}")
    @ApiOperation(value = "获取活动报名详情")
    public AjaxResults getActivityById(@PathVariable("id") String id) {
        KActivityPurchase kActivityPurchase=  kActivityPurchaseService.selectKActivityPurchaseById(id);
        kActivityPurchase.setRemainingQuota2(Math.toIntExact(kActivityPurchase.getTotalQuota() - kActivityPurchase.getSold()));
//        kActivityPurchase.setActivityVenueApp(kCourseLocationService.selectKCourseLocationById(kActivityPurchase.getCourseLocation()).getName());
        kActivityPurchase.setAgeApp(kAgeGroupService.selectKAgeGroupById(kActivityPurchase.getAgeGroup()).getName());
//        kActivityPurchase.setClassesApp(kCourseClassesService.selectKCourseClassesById(kActivityPurchase.getCourseClasses()).getName());
//        kActivityPurchase.setClassApp(kClassCategoryService.selectKClassCategoryById(kActivityPurchase.getClassCategory()).getName());
        return   AjaxResults.success(kActivityPurchase);
    }
    /**
     * 删除订单
     */
    @DeleteMapping(value = "/weixin/deletePurchasedCourses/{id}")
    @ApiOperation(value = "删除已购课程")
    public AjaxResults deletePurchasedCourses(@PathVariable("id") String id)
    {
        return AjaxResults.success(kCoursePurchaseService.deleteKCoursePurchaseById(id));
    }

}
